package cn.edu.glut.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import cn.edu.glut.interceptor.LoginInterceptor;
import cn.edu.glut.interceptor.PrivilegeInterceptor;

/**
 * 
 * @author Caizhfy
 * @email caizhfy@163.com
 * @createTime 2017年12月6日
 * @description http://blog.csdn.net/catoop/article/details/50501706参考静态
 *
 */
@Configuration
public class MyWebConfiguration extends WebMvcConfigurerAdapter {
	@Autowired
	private LoginInterceptor loginInterceptor;
	@Autowired
	private PrivilegeInterceptor privilegeInterceptor;
	
	//静态文件配置
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
		registry.addResourceHandler("/user-file/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/user-file/");
		super.addResourceHandlers(registry);
	}
	

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("/index");//公共
		registry.addViewController("/index").setViewName("/index"); //公共
		registry.addViewController("/main").setViewName("/main"); //公共
		registry.addViewController("/major").setViewName("/major"); //管理员
		registry.addViewController("/select").setViewName("/select"); //学生
		registry.addViewController("/account").setViewName("/account"); //公共
		
		registry.addViewController("/user/student").setViewName("/user/student"); //管理
		registry.addViewController("/user/teacher").setViewName("/user/teacher"); //管理
		registry.addViewController("/user/teacherPaper").setViewName("/user/teacherPaper"); //管理
		registry.addViewController("/user/studentPaper").setViewName("/user/studentPaper"); //管理
		
		registry.addViewController("/paper/list").setViewName("/paper/list"); //管理，老师
		registry.addViewController("/paper/add").setViewName("/paper/add");//老师
		registry.addViewController("/paper/edit").setViewName("/paper/edit");//编辑
		registry.addViewController("/paper/selectStudent").setViewName("/paper/selectStudent");//管理，老师
		registry.addViewController("/paper/detail").setViewName("/paper/detail");//公共
		registry.addViewController("/paper/student").setViewName("/paper/student");//管理，老师

		
		registry.addViewController("/error/info").setViewName("/error/info");//公共
		
		registry.addViewController("/login").setViewName("/login");//公共
		super.addViewControllers(registry);
	}



	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 拦截规则：除了login，其他都拦截判断
		registry.addInterceptor(loginInterceptor)
			.addPathPatterns("/**")
//			.excludePathPatterns("/assets/**", "/public/**")
			.excludePathPatterns("/usr/signin")
			.excludePathPatterns("/static/**")
			.excludePathPatterns("/login");
		/*registry.addInterceptor(privilegeInterceptor)
			.addPathPatterns("/**")
			.excludePathPatterns("/static/**")
			.excludePathPatterns("/login.html")
			.excludePathPatterns("/usr/signin");*/
		super.addInterceptors(registry);
	}
	
	//错误页面设置
	/*@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {

	    return new EmbeddedServletContainerCustomizer() {
	        @Override
	        public void customize(ConfigurableEmbeddedServletContainer container) {

	            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401.html");
	            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");
	            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");
	            container.addErrorPages(error401Page, error404Page, error500Page);
	        }
	    };
	}*/

}
